package com.example.concurrentprogramming8016.thread.Atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author liyanhao
 * @Date 2023-01-03 21:24
 */
public class AtomicIntegerFieldUpdaterTest2 {
    volatile int a = 0;
    //使用AtomicIntegerFieldUpdater 对a进行修饰
    private static final AtomicIntegerFieldUpdater atomicIntegerFieldUpdater =
            AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest2.class, "a");
    //测试使用100个线程修改0的值
    public static void main(String[] args) {
        AtomicIntegerFieldUpdaterTest2 atomicIntegerFieldUpdaterTest2 = new AtomicIntegerFieldUpdaterTest2();
        for (int i = 0; i < 100; i++) {
            final int j = i;
            new Thread(()->{
                System.out.println("i=" + j + ",a=" + atomicIntegerFieldUpdater.incrementAndGet(atomicIntegerFieldUpdaterTest2));
            }).start();
        }
    }

}
